home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World Komputer 2010 April
/
PCWorld0410.iso
/
pluginy Firefox
/
9675
/
9675.xpi
/
chrome
/
content
/
simpletimer-fileIO.js
< prev
next >
Wrap
Text File
|
2009-07-29
|
4KB
|
133 lines
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Initial Developer of the Original Code is
* MonkeeSage
*
* Contributor(s):
* George Bradt
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
var SimpleTimerFileIO = {
localfileCID: "@mozilla.org/file/local;1",
localfileIID: Components.interfaces.nsILocalFile,
finstreamCID: "@mozilla.org/network/file-input-stream;1",
finstreamIID: Components.interfaces.nsIFileInputStream,
foutstreamCID: "@mozilla.org/network/file-output-stream;1",
foutstreamIID: Components.interfaces.nsIFileOutputStream,
cinstreamCID: "@mozilla.org/intl/converter-input-stream;1",
cinstreamIID: Components.interfaces.nsIConverterInputStream,
coutstreamCID: "@mozilla.org/intl/converter-output-stream;1",
coutstreamIID: Components.interfaces.nsIConverterOutputStream,
open: function(path) {
try {
var file = Components.classes[this.localfileCID].
createInstance(this.localfileIID);
file.initWithPath(path);
return file;
}
catch(e) {
return false;
}
},
read: function(file) {
try {
var data = "";
var fiStream = Components.classes[this.finstreamCID].
createInstance(this.finstreamIID);
var ciStream = Components.classes[this.cinstreamCID].
createInstance(this.cinstreamIID);
fiStream.init(file, -1, 0, 0);
ciStream.init(fiStream, "UTF-8", 0, 0); // 3rd parm, use default buffer size of 8K.
var str = {};
while ( ciStream.readString(-1, str) !== 0 ) {
// Read the stream in 8K chunks.
data += str.value;
}
// This also closes fiStream.
ciStream.close();
return data;
}
catch(e) {
return false;
}
},
write: function(file, data, mode) {
try {
var foStream = Components.classes[this.foutstreamCID].
createInstance(this.foutstreamIID);
var coStream = Components.classes[this.coutstreamCID].
createInstance(this.coutstreamIID);
var flags = 0x02 | 0x08 | 0x20; // wronly | create | truncate
if ( mode === "a" ) {
flags = 0x02 | 0x10; // wronly | append
}
foStream.init(file, flags, 0664, 0);
coStream.init(foStream, "UTF-8", 0, 0);
coStream.writeString(data);
// This also closes foStream.
coStream.close();
return true;
}
catch(e) {
return false;
}
},
create: function(file) {
try {
file.create(0x00, 0664);
return true;
}
catch(e) {
return false;
}
},
// Debug messages to console.
debug: function(aMsg) {
setTimeout(function() { throw new Error("[debug] " + aMsg); }, 0);
}
};